const req = require('../../utils/apifm-wxapi')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: wx.getStorageSync('userinfo')||null,
    userData: [{
        title: '余额',
        num: 0.00
      },
      {
        title: '冻结',
        num: 0.00
      },
      {
        title: '积分',
        num: 0.00
      },
      {
        title: '成长值',
        num: 0.00
      }
    ],
    navigate: [{
        text: '优惠买单',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      },
      {
        text: '资金明细',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '成为分销商',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '申请发票',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '发票记录',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '收货地址',
        img: "/images/icon/next.png",
        page: '/pages/address/address',
      }, {
        text: '领券中心',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '我的收藏',
        img: "/images/icon/next.png",
        page: '/pages/collect/collect',
      }, {
        text: '每日签到',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '积分券兑换积分',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      },
      {
        text: '积分兑换成长值',
        img: "/images/icon/next.png",
        page: '/pages/logs/logs',
      }, {
        text: '关于我们',
        img: "/images/icon/next.png",
      }, {
        text: '绑定手机',
        img: "/images/icon/next.png",
      }, {
        text: '清除缓存',
        img: "/images/icon/next.png",
      }, {
        text: '退出登录',
        img: "/images/icon/next.png",
      }
    ],
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  goOrder(e) {
    console.log(1111)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },
  getUserInfo(e) {
    console.log(e)
    if (e.detail.userInfo) {
      wx.login({
        complete: (res) => {
          console.log(res.code)
          var code =res.code
          req.login_wx(code).then(res=>{
            console.log(res)
            wx.setStorageSync('Token', res.data.token)
            wx.setStorageSync('uid', res.data.uid)
          })
        },
      })
      //用户按了允许授权按钮
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      console.log(this.data.userinfo)
      wx.setStorageSync('userinfo', e.detail.userInfo)
      var userinfo=wx.getStorageSync('userinfo')
      this.setData({
        isHide: false,
        userinfo:userinfo
      });
      console.log(this.data.userinfo)

    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function(res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
  // 退出登录
  LoginOut(e){
    console.log(e.currentTarget.dataset.key)
    var token = wx.getStorageSync('Token')
    if(e.currentTarget.dataset.key=="退出登录"||e.currentTarget.dataset.key=="清除缓存"){
      req.loginout(token).then((res)=>{
      console.log(res)
      if(res.code==0){
        wx.removeStorageSync('Token')
        wx.removeStorageSync('uid')
        wx.removeStorageSync('userinfo')
        wx.removeStorageSync('number')
      }
    })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var userinfo= wx.getStorageSync('userinfo')
    var Token = wx.getStorageSync('Token')
    var number = wx.getStorageSync('number')
    if(number!=0){
      wx.setTabBarBadge({
        index: 2,
        text: String(number),
      })
    }else{
      wx.removeTabBarBadge({
        index: 2,
      })
    }
    if(userinfo&&Token){
      this.setData({
        userinfo:userinfo,
        token:Token
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})